package com.nmwlw.demo.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.filled.Done
import androidx.compose.material3.Button
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconToggleButton
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.nmwlw.demo.data.DataSource.BaudRateOptions
import com.nmwlw.demo.data.DataSource.ComOptions
import com.nmwlw.demo.data.viewModle.ComAssistantModel

@Preview(device = Devices.AUTOMOTIVE_1024p,
    widthDp = 2173,
    heightDp = 	1223,
    showBackground = true)
@Composable
fun ComAssistantPreview() {
//    ComAssistantScreen()
    DemoScreen()
}




@Composable
fun DemoScreen(
    comAssistantModel: ComAssistantModel = viewModel()
) {
    val comAssistantState by comAssistantModel.comAssistantState.collectAsState()
    // 控制台输入
    var controlInput = comAssistantState.currentContext

    var comAText = comAssistantState.comAText
    var comBText = comAssistantState.comBText
    var comCText = comAssistantState.comCText
    var comDText = comAssistantState.comDText
    // 打开串口
    var comAChecked = comAssistantState.comAChecked
    var comBChecked = comAssistantState.comBChecked
    var comCChecked = comAssistantState.comCChecked
    var comDChecked = comAssistantState.comDChecked

    var comAExpanded = comAssistantState.comAExpanded
    var comBExpanded = comAssistantState.comBExpanded
    var comCExpanded = comAssistantState.comCExpanded
    var comDExpanded = comAssistantState.comDExpanded

    var comASelectedOption = comAssistantState.comASelectedOption
    var comBSelectedOption = comAssistantState.comBSelectedOption
    var comCSelectedOption = comAssistantState.comCSelectedOption
    var comDSelectedOption = comAssistantState.comDSelectedOption

    var baudAExpanded = comAssistantState.baudAExpanded
    var baudBExpanded = comAssistantState.baudBExpanded
    var baudCExpanded = comAssistantState.baudCExpanded
    var baudDExpanded = comAssistantState.baudDExpanded

    var baudASelectedOption = comAssistantState.baudASelectedOption
    var baudBSelectedOption = comAssistantState.baudBSelectedOption
    var baudCSelectedOption = comAssistantState.baudCSelectedOption
    var baudDSelectedOption = comAssistantState.baudDSelectedOption

    Column(
    ){
        Row(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f)
        ) {
            // 接收显示区域
            BasicTextField(
                value = controlInput,
                onValueChange = {},
                modifier = Modifier
                    .weight(1f)
                    .fillMaxHeight()
                    .padding(8.dp)
                    .border(
                        width = 1.dp,
                        color = Color.Gray,
                        shape = RoundedCornerShape(4.dp)
                    )
                    .background(Color.White),
                enabled = false,
                readOnly = true,
                textStyle = LocalTextStyle.current.copy(
                    fontSize = 14.sp,
                    color = Color.Black
                ),
                // 用于定义和管理组件的交互状态（如点击、悬停等)创建一个可变的交互源。
                // 虽然在代码中没有明确使用交互状态的功能，但它通常用于自定义组件的交互反馈（如高亮显示、点击效果等）
                interactionSource = remember { MutableInteractionSource() }
            )
            Column(
                modifier = Modifier.fillMaxHeight()
            ) {
                Button(
                    onClick = {
                        comAssistantModel.cleanContext()
                    }
                ) {
                    Text("清除")
                }
                Switch(
                    modifier = Modifier,
                    checked = comAssistantState.enableHex,
                    onCheckedChange = {
                        comAssistantModel.changeContextType()
                    }
                )
            }
        }
        Row(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f)
        ) {
            // A
            Column (
                modifier = Modifier
                    .weight(1f)
                    .border(1.dp, Color.Black, RoundedCornerShape(4.dp))
            ){
                BasicTextField(
                    modifier = Modifier
                        .weight(1f)
                        .fillMaxWidth()
                        .wrapContentHeight()
                        .padding(8.dp),
                    textStyle = LocalTextStyle.current.copy(
                        fontSize = 14.sp,
                        color = Color.Black
                    ),
                    keyboardOptions = KeyboardOptions(
                        imeAction = ImeAction.Done,
                        keyboardType = KeyboardType.Text
                    ),
                    value = comAText,
                    onValueChange = {
//                        comAText = it
                        comAssistantModel.comInputText("A",it)
                                    },
                    enabled = true,
                )
                Spacer(modifier = Modifier.height(8.dp))
                OptionsMenuBox(
                    modifier = Modifier
                        .fillMaxWidth(),
                    options = ComOptions,
                    expanded = comAExpanded,
                    onExpandedChange = {
//                        comAExpanded= it
                        comAssistantModel.changeExpanded("A",0)
                                       },
                    selectedOption = comASelectedOption,
                    onSelectedOptionChange = { comAssistantModel.optionsSelect("A",it,0)}
                )
                Row(
                    modifier = Modifier
                        .fillMaxWidth(),
                ){
                    OptionsMenuBox(
                        modifier = Modifier
                            .fillMaxWidth(0.9f)
                            .weight(0.9f),
                        options = BaudRateOptions,
                        expanded = baudAExpanded,
                        onExpandedChange = {
//                            baudAExpanded= it
                            comAssistantModel.changeExpanded("A",1)
                                           },
                        selectedOption = baudASelectedOption,
                        onSelectedOptionChange = {
//                            baudASelectedOption = it
                            comAssistantModel.optionsSelect("A",it,1)
                        }
                    )
                    IconToggleButton(
                        modifier = Modifier
                            .weight(0.1f)
                            .fillMaxWidth(),
                        checked = comAChecked,
                        onCheckedChange = {
//                            comAChecked = it
                            comAssistantModel.changeExpanded("A",2)
                            comAssistantModel.comOpen("A")
                        }
                    ) {
                        if (comAChecked) {
                            Icon(
                                imageVector = Icons.Default.Done,
                                contentDescription = "Checked",
                                tint = Color.Black
                            )
                        } else {
                            Icon(
                                imageVector = Icons.Default.Clear,
                                contentDescription = "Unchecked",
                            )
                        }
                    }
                }
                Row (
                    modifier = Modifier
                        .fillMaxWidth(),
                ){
                    Button(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(8.dp),
                        onClick = {
                            comAssistantModel.btnSend("A")
                        }
                    ) {
                        Text(text = "发送")
                    }
                }
            }
//            // B
//            Column (
//                modifier = Modifier
//                    .weight(1f)
//                    .border(1.dp, Color.Black, RoundedCornerShape(4.dp))
//            ){
//                BasicTextField(
//                    modifier = Modifier
//                        .weight(1f)
//                        .fillMaxWidth()
//                        .wrapContentHeight()
//                        .padding(8.dp),
//                    textStyle = LocalTextStyle.current.copy(
//                        fontSize = 14.sp,
//                        color = Color.Black
//                    ),
//                    keyboardOptions = KeyboardOptions(
//                        imeAction = ImeAction.Done,
//                        keyboardType = KeyboardType.Text
//                    ),
//                    value = comBText,
//                    onValueChange = {
////                        comBText = it
//                        comAssistantModel.comInputText("B",it)
//                                    },
//                    enabled = true,
//                )
//                Spacer(modifier = Modifier.height(8.dp))
//                OptionsMenuBox(
//                    modifier = Modifier
//                        .fillMaxWidth(),
//                    options = ComOptions,
//                    expanded = comBExpanded,
//                    onExpandedChange = {
////                        comBExpanded= it
//                        comAssistantModel.changeExpanded("B",0)
//                                       },
//                    selectedOption = comBSelectedOption,
//                    onSelectedOptionChange = {
////                        comBSelectedOption = it
//                        comAssistantModel.optionsSelect("B",it,0)
//                    }
//                )
//                Row(
//                    modifier = Modifier
//                        .fillMaxWidth(),
//                ){
//                    OptionsMenuBox(
//                        modifier = Modifier
//                            .fillMaxWidth(0.9f)
//                            .weight(0.9f),
//                        options = BaudRateOptions,
//                        expanded = baudBExpanded,
//                        onExpandedChange = {
////                            baudBExpanded= it
//                            comAssistantModel.changeExpanded("B",1)
//                                           },
//                        selectedOption = baudBSelectedOption,
//                        onSelectedOptionChange = {
////                            baudBSelectedOption = it
//                            comAssistantModel.optionsSelect("B",it,1)
//                        }
//                    )
//                    IconToggleButton(
//                        modifier = Modifier
//                            .weight(0.1f)
//                            .fillMaxWidth(),
//                        checked = comBChecked,
//                        onCheckedChange = {
////                            comBChecked = it
//                            comAssistantModel.changeExpanded("B",2)
//                        }
//                    ) {
//                        if (comBChecked) {
//                            Icon(
//                                imageVector = Icons.Default.Done,
//                                contentDescription = "Checked",
//                                tint = Color.Black
//                            )
//                        } else {
//                            Icon(
//                                imageVector = Icons.Default.Clear,
//                                contentDescription = "Unchecked",
//                            )
//                        }
//                    }
//                }
//                Row (
//                    modifier = Modifier
//                        .fillMaxWidth(),
//                ){
//                    Button(
//                        modifier = Modifier
//                            .fillMaxWidth()
//                            .padding(8.dp),
//                        onClick = {
//                            comAssistantModel.btnSend("B")
//                        }
//                    ) {
//                        Text(text = "发送")
//                    }
//                }
//            }
//            // C
//            Column (
//                modifier = Modifier
//                    .weight(1f)
//                    .border(1.dp, Color.Black, RoundedCornerShape(4.dp))
//            ){
//                BasicTextField(
//                    modifier = Modifier
//                        .weight(1f)
//                        .fillMaxWidth()
//                        .wrapContentHeight()
//                        .padding(8.dp),
//                    textStyle = LocalTextStyle.current.copy(
//                        fontSize = 14.sp,
//                        color = Color.Black
//                    ),
//                    keyboardOptions = KeyboardOptions(
//                        imeAction = ImeAction.Done,
//                        keyboardType = KeyboardType.Text
//                    ),
//                    value = comCText,
//                    onValueChange = {
////                        comCText = it
//                        comAssistantModel.comInputText("C",it)
//                                    },
//                    enabled = true,
//                )
//                Spacer(modifier = Modifier.height(8.dp))
//                OptionsMenuBox(
//                    modifier = Modifier
//                        .fillMaxWidth(),
//                    options = ComOptions,
//                    expanded = comCExpanded,
//                    onExpandedChange = {
////                        comCExpanded= it
//                        comAssistantModel.changeExpanded("C",0)
//                                       },
//                    selectedOption = comCSelectedOption,
//                    onSelectedOptionChange = {
////                        comCSelectedOption = it
//                        comAssistantModel.optionsSelect("C",it,0)
//                    }
//                )
//                Row(
//                    modifier = Modifier
//                        .fillMaxWidth(),
//                ){
//                    OptionsMenuBox(
//                        modifier = Modifier
//                            .fillMaxWidth(0.9f)
//                            .weight(0.9f),
//                        options = BaudRateOptions,
//                        expanded = baudCExpanded,
//                        onExpandedChange = {
//                            comAssistantModel.changeExpanded("C",1)
//                                           },
//                        selectedOption = baudCSelectedOption,
//                        onSelectedOptionChange = {
////                            baudCSelectedOption = it
//                            comAssistantModel.optionsSelect("C",it,1)
//                        }
//                    )
//                    IconToggleButton(
//                        modifier = Modifier
//                            .weight(0.1f)
//                            .fillMaxWidth(),
//                        checked = comCChecked,
//                        onCheckedChange = {
////                            comCChecked = it
//                            comAssistantModel.changeExpanded("C",2)
//                        }
//                    ) {
//                        if (comCChecked) {
//                            Icon(
//                                imageVector = Icons.Default.Done,
//                                contentDescription = "Checked",
//                                tint = Color.Black
//                            )
//                        } else {
//                            Icon(
//                                imageVector = Icons.Default.Clear,
//                                contentDescription = "Unchecked",
//                            )
//                        }
//                    }
//                }
//                Row (
//                    modifier = Modifier
//                        .fillMaxWidth(),
//                ){
//                    Button(
//                        modifier = Modifier
//                            .fillMaxWidth()
//                            .padding(8.dp),
//                        onClick = {
//                            comAssistantModel.btnSend("C")
//                        }
//                    ) {
//                        Text(text = "发送")
//                    }
//                }
//            }
//            // D
//            Column (
//                modifier = Modifier
//                    .weight(1f)
//                    .border(1.dp, Color.Black, RoundedCornerShape(4.dp))
//            ){
//                BasicTextField(
//                    modifier = Modifier
//                        .weight(1f)
//                        .fillMaxWidth()
//                        .wrapContentHeight()
//                        .padding(8.dp),
//                    textStyle = LocalTextStyle.current.copy(
//                        fontSize = 14.sp,
//                        color = Color.Black
//                    ),
//                    keyboardOptions = KeyboardOptions(
//                        imeAction = ImeAction.Done,
//                        keyboardType = KeyboardType.Text
//                    ),
//                    value = comDText,
//                    onValueChange = {
////                        comDText = it
//                        comAssistantModel.comInputText("D",it)
//                                    },
//                    enabled = true,
//                )
//                Spacer(modifier = Modifier.height(8.dp))
//                OptionsMenuBox(
//                    modifier = Modifier
//                        .fillMaxWidth(),
//                    options = ComOptions,
//                    expanded = comDExpanded,
//                    onExpandedChange = {
////                        comDExpanded= it
//                        comAssistantModel.changeExpanded("D",0)
//                                       },
//                    selectedOption = comDSelectedOption,
//                    onSelectedOptionChange = {
////                        comDSelectedOption = it
//                        comAssistantModel.optionsSelect("D",it,0)
//                    }
//                )
//                Row(
//                    modifier = Modifier
//                        .fillMaxWidth(),
//                ){
//                    OptionsMenuBox(
//                        modifier = Modifier
//                            .fillMaxWidth(0.9f)
//                            .weight(0.9f),
//                        options = BaudRateOptions,
//                        expanded = baudDExpanded,
//                        onExpandedChange = {
//                            comAssistantModel.changeExpanded("D",1)
//                                           },
//                        selectedOption = baudDSelectedOption,
//                        onSelectedOptionChange = {
////                            baudDSelectedOption = it
//                            comAssistantModel.optionsSelect("D",it,1)
//                        }
//                    )
//                    IconToggleButton(
//                        modifier = Modifier
//                            .weight(0.1f)
//                            .fillMaxWidth(),
//                        checked = comDChecked,
//                        onCheckedChange = {
////                            comDChecked = it
//                            comAssistantModel.changeExpanded("D",2)
//                        }
//                    ) {
//                        if (comDChecked) {
//                            Icon(
//                                imageVector = Icons.Default.Done,
//                                contentDescription = "Checked",
//                                tint = Color.Black
//                            )
//                        } else {
//                            Icon(
//                                imageVector = Icons.Default.Clear,
//                                contentDescription = "Unchecked",
//                            )
//                        }
//                    }
//                }
//                Row (
//                    modifier = Modifier
//                        .fillMaxWidth(),
//                ){
//                    Button(
//                        modifier = Modifier
//                            .fillMaxWidth()
//                            .padding(8.dp),
//                        onClick = {
//                            comAssistantModel.btnSend("D")
//                        }
//                    ) {
//                        Text(text = "发送")
//                    }
//                }
//            }
        }

    }
}

/**
 * 下拉组件
 */
@Composable
fun OptionsMenuBox(
    modifier: Modifier = Modifier,
    options : List<String>,
    expanded : Boolean,
    onExpandedChange : (Boolean) -> Unit,
    selectedOption : String,
    onSelectedOptionChange: (String) -> Unit
) {

    ExposedDropdownMenuBox(
        expanded = expanded,
        onExpandedChange = onExpandedChange
    ) {
        TextField(
            value = selectedOption,
            onValueChange = {},
            readOnly = true,
            trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded) },
            modifier = modifier
                .menuAnchor() // 焦点处理
        )
        ExposedDropdownMenu(
            expanded = expanded,
            onDismissRequest = { onExpandedChange(false) }
        ) {
            options.forEach { option ->
                DropdownMenuItem(
                    text = { Text(option) },
                    onClick = {
                        onSelectedOptionChange(option)
                        onExpandedChange(false)
                    }
                )
            }
        }
    }
}





